import axios from 'axios';

const API_BASE_URL = 'http://localhost:9011';

export const searchDocuments = async (query: string, topK: number = 5) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/api/knowledge/search`, {
      params: { query, topK },
    });
    return response.data;
  } catch (error) {
    console.error('Error searching documents:', error);
    throw error;
  }
};

export const addDocument = async (file: File) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    const response = await axios.post(`${API_BASE_URL}/api/knowledge/addDocument`, formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    });
    return response.data;
  } catch (error) {
    console.error('Error adding document:', error);
    throw error;
  }
};

// 获取已嵌入的文档列表
export const getDocuments = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/api/knowledge/getDocuments`);
    return response.data;
  } catch (error) {
    console.error('Error getting documents:', error);
    throw error;
  }
};
